Skip to main content

Detect Pattern of Length M Repeated K or More Times

  1. Detect Pattern of Length M Repeated K or More Times

Given an array of positive integers arr, find a pattern of length m that is repeated k or more times.

A pattern is a subarray (consecutive sub-sequence) that consists of one or more values, repeated multiple times consecutively without overlapping. A pattern is defined by its length and the number of repetitions.

Return true if there exists a pattern of length m that is repeated k or more times, otherwise return false.

 bool containsPattern(vector<int>& arr, int m, int k) {

int cnt=0;
for(int i=0;i+m < arr.size(); i++){

if(arr[i]!=arr[i+m]){
cnt=0;
}
cnt += (arr[i] == arr[i+m]);
if(cnt == (k-1)*m)
return true;

}
return false;
}